AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:Initialize()
	self:SetModel("models/weapons/w_rocket_launcher.mdl")
	self:DrawShadow(false)
	self.NextAttack = 0
end

function ENT:Think()
end

function ENT:KeyPress(pl, vehicle, key)
	if key == IN_ATTACK and vehicle.Powerups[1] == self and self.NextAttack <= CurTime() then
		local pos = self:GetPos()
		self.NextAttack = CurTime() + 2

		local ent = ents.Create("projectile_rocket")
		ent:SetPos(pos)
		ent:SetAngles(self:GetAngles())
		ent:SetOwner(vehicle)
		ent:SetColor(vehicle:GetColor())
		ent:Spawn()
		ent.Owner = pl

		ent.Heading = -1 * self:GetForward()
		ent:GetPhysicsObject():SetVelocityInstantaneous(SCALE * math.max(2500, vehicle:GetVelocity():Length() * 0.6) * ent.Heading + vehicle:GetVelocity() * 0.4)

		self:EmitSound("Weapon_RPG.Single")

		if self:GetAmmo() <= 1 then
			self:CreateDebris()
			self:Remove()
		else
			self:SetAmmo(self:GetAmmo() - 1)
		end

		return true
	end
end

function ENT:OnRemove()
	local vehicle = self:GetOwner()
	if vehicle:IsValid() then
		vehicle:RemovePowerup(self)
	end
end

function ENT:VehicleSet(eVehicle, bExists)
	--eVehicle:RemoveStatus("weapon_*", true, true, "status_weapon_rocket")
	if not bExists then
		eVehicle:AddPowerup(self)
	end

	self:SetAmmo(MAX_ROCKETS)

	local ang = eVehicle:GetAngles()
	self:SetPos(eVehicle:GetPos() + (ang:Up() * 10 + ang:Forward() * 64 + ang:Right() * 25) * SCALE)
	ang:RotateAroundAxis(ang:Up(), 180)
	self:SetAngles(ang)
end
